<script setup lang="ts">
import ArticleItem from '@/components/ArticleItem.vue'
import { useArticleStore } from '@/stores/article'
import { storeToRefs } from 'pinia'

const articleStore = useArticleStore()
const { articleList, isLoading, isFinished, sorter } = storeToRefs(articleStore)

// 获取文章列表
const getArticles = async () => {
  await articleStore.getArticles()
}

// 切换排序字段
const changeSorter = async (value: string | null) => {
  await articleStore.changeSorter(value)
}
</script>

<template>
  <div class="home-view">
    <!-- 顶部导航栏 -->
    <nav class="my-nav van-hairline--bottom">
      <a
        href="javascript:;"
        @click="changeSorter('weight_desc')"
        :class="{ active: sorter === 'weight_desc' }"
      >
        推荐
      </a>
      <a href="javascript:;" @click="changeSorter(null)" :class="{ active: sorter === null }">
        最新
      </a>
      <div class="logo">
        <div class="logo-text">面经</div>
      </div>
    </nav>

    <!-- 文章列表 -->
    <van-list
      class="article-list"
      v-model:loading="isLoading"
      :finished="isFinished"
      @load="getArticles"
      finished-text="没有更多了"
    >
      <!-- 文章项 -->
      <ArticleItem v-for="item in articleList" :key="item.id" :item="item" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.home-view {
  margin-bottom: 50px;
  margin-top: 44px;
}

.my-nav {
  height: 44px;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  z-index: 999;
  background: #fff;
  display: flex;
  align-items: center;

  > a {
    color: #999;
    font-size: 14px;
    line-height: 44px;
    margin-left: 20px;
    position: relative;
    transition: all 0.3s;
    text-decoration: none;

    &::after {
      content: '';
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      bottom: 0;
      width: 0;
      height: 2px;
      background: #222;
      transition: all 0.3s;
    }

    &.active {
      color: #222;

      &::after {
        width: 14px;
      }
    }
  }

  .logo {
    flex: 1;
    display: flex;
    justify-content: flex-end;

    .logo-text {
      font-size: 18px;
      font-weight: bold;
      color: var(--van-primary-color);
      margin-right: 16px;
      line-height: 44px;
    }
  }
}
</style>
